package com.utils;

public class StringUtil {

    // 判断字符串是否为空
    public static boolean isEmpty(String str) {
        return str == null || str.trim().isEmpty();
    }

    // 判断字符串是否为非空
    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    // 去除字符串首尾的空白字符
    public static String trim(String str) {
        return str == null ? null : str.trim();
    }

    // 将字符串转换为大写
    public static String toUpperCase(String str) {
        return str == null ? null : str.toUpperCase();
    }

    // 将字符串转换为小写
    public static String toLowerCase(String str) {
        return str == null ? null : str.toLowerCase();
    }

    // 判断字符串是否为数字
    public static boolean isNumeric(String str) {
        if (isEmpty(str)) {
            return false;
        }
        return str.matches("\\d+");
    }

    // 判断字符串是否为有效的邮箱地址
    public static boolean isValidEmail(String str) {
        if (isEmpty(str)) {
            return false;
        }
        String emailRegex = "^[A-Za-z0-9+_.-]+@(.+)$";
        return str.matches(emailRegex);
    }

    // 将字符串数组拼接成一个字符串，中间用指定的分隔符分隔
    public static String join(String[] array, String separator) {
        if (array == null || array.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            if (i < array.length - 1) {
                sb.append(separator);
            }
        }
        return sb.toString();
    }

    // 将字符串的首字母大写
    public static String capitalize(String str) {
        if (isEmpty(str)) {
            return str;
        }
        return str.substring(0, 1).toUpperCase() + str.substring(1).toLowerCase();
    }

    // 将字符串的首字母小写
    public static String uncapitalize(String str) {
        if (isEmpty(str)) {
            return str;
        }
        return str.substring(0, 1).toLowerCase() + str.substring(1);
    }

    // 判断两个字符串是否相等（忽略大小写）
    public static boolean equalsIgnoreCase(String str1, String str2) {
        return str1 == null ? str2 == null : str1.equalsIgnoreCase(str2);
    }

    // 将驼峰命名法转换为下划线命名法
    public static String camelToSnake(String str) {
        if (isEmpty(str)) {
            return str;
        }
        String regex = "([a-z])([A-Z]+)";
        String replacement = "$1_$2";
        return str.replaceAll(regex, replacement).toLowerCase();
    }

    // 将下划线命名法转换为驼峰命名法
    public static String snakeToCamel(String str) {
        if (isEmpty(str)) {
            return str;
        }
        String[] parts = str.split("_");
        StringBuilder camelCaseString = new StringBuilder(parts[0]);
        for (int i = 1; i < parts.length; i++) {
            camelCaseString.append(capitalize(parts[i]));
        }
        return camelCaseString.toString();
    }

    // 补全字符串
    public static String padString(String str, char padChar, int length, PadDirection direction) {
        if (str == null) {
            str = "";
        }
        int totalPadding = length - str.length();
        if (totalPadding <= 0) {
            return str;
        }
        StringBuilder sb = new StringBuilder();
        switch (direction) {
            case LEFT:
                for (int i = 0; i < totalPadding; i++) {
                    sb.append(padChar);
                }
                sb.append(str);
                break;
            case RIGHT:
                sb.append(str);
                for (int i = 0; i < totalPadding; i++) {
                    sb.append(padChar);
                }
                break;
            case CENTER:
                int padStart = totalPadding / 2;
                int padEnd = totalPadding - padStart;
                for (int i = 0; i < padStart; i++) {
                    sb.append(padChar);
                }
                sb.append(str);
                for (int i = 0; i < padEnd; i++) {
                    sb.append(padChar);
                }
                break;
        }
        return sb.toString();
    }

    // 补全方向枚举
    public enum PadDirection {
        LEFT,
        RIGHT,
        CENTER
    }
}
